package com.xkdx.guangguang.fragment.comment;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class BindSinaModule extends AbsModule {
	Context context;
	public BindUser bindUser;
	JSONArray jsonArray;
	JSONObject jsonObj;

	public BindSinaModule(Context context) {
		this.context = context;
	}

	@Override
	public void parseData() throws Exception {
		backJson = (JSONArray) result;
		try {
			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
//				String sortOfAct = jsonObj.getString("Act");
//				if (sortOfAct.equals("bindSinaWeibo")) {
				jsonArray = (JSONArray) result;
				jsonObj = (JSONObject) jsonArray.get(0);
					bindUser = parseBindSina(jsonObj.getJSONObject("Content"));
//				}

			}
		} catch (Exception e) {
			throw e;
		}

	}

	private BindUser parseBindSina(JSONObject person) throws Exception {
		SinaDetailInfo sinaDetailinfo = new SinaDetailInfo();
		BindUser bindUser = new BindUser();
		try {
			JSONObject stut = (JSONObject) person.get("Result");
			JSONObject detail = (JSONObject) stut.getJSONObject("DetailInfo");
			sinaDetailinfo.setUserID(detail.getString("UserID"));
			sinaDetailinfo.setSex(detail.getString("Sex"));
			sinaDetailinfo.setSinaWeiboNickName(detail
					.getString("SinaWeiboNickName"));
			sinaDetailinfo
					.setSinaWeiboToken(detail.getString("SinaWeiboToken"));
			sinaDetailinfo.setSinaWeiboExpireTime(detail
					.getString("SinaWeiboExpireTime"));
			sinaDetailinfo.setSinaWeiboUserID(detail
					.getString("SinaWeiboUserID"));

			bindUser.setErrorCode(stut.getString("ErrorCode"));
			bindUser.setMessage(stut.getString("Message"));
			bindUser.setType(stut.getString("Type"));
			bindUser.setStatus(stut.getString("Status"));
			bindUser.setSinadetailinfo(sinaDetailinfo);
			if ("Success".equals(stut.getString("Status"))) {
				SharePrefenceUtil sinaSP = new SharePrefenceUtil(context,
						IConstants.SP_USER);
				sinaSP.setUserID(detail.getString("UserID"));
				sinaSP.setUserSex(detail.getString("Sex"));
				sinaSP.setSinaWeiboUserID(detail.getString("SinaWeiboUserID"));
				sinaSP.setSinaWeiboNickName(detail
						.getString("SinaWeiboNickName"));
				sinaSP.setSinaWeiboToken(detail.getString("SinaWeiboToken"));
				sinaSP.setSinaWeiboExpireTime(detail
						.getString("SinaWeiboExpireTime"));
			}
		} catch (JSONException e) {
			throw e;
		}
		return bindUser;
	}

	public class SinaDetailInfo implements Serializable {
		private String UserID;
		private String Sex;
		private String SinaWeiboUserID;
		private String SinaWeiboNickName;
		private String SinaWeiboToken;
		private String SinaWeiboExpireTime;

		public String getUserID() {
			return UserID;
		}

		public void setUserID(String userID) {
			UserID = userID;
		}

		public String getSex() {
			return Sex;
		}

		public void setSex(String sex) {
			Sex = sex;
		}

		public String getSinaWeiboUserID() {
			return SinaWeiboUserID;
		}

		public void setSinaWeiboUserID(String sinaWeiboUserID) {
			SinaWeiboUserID = sinaWeiboUserID;
		}

		public String getSinaWeiboNickName() {
			return SinaWeiboNickName;
		}

		public void setSinaWeiboNickName(String sinaWeiboNickName) {
			SinaWeiboNickName = sinaWeiboNickName;
		}

		public String getSinaWeiboToken() {
			return SinaWeiboToken;
		}

		public void setSinaWeiboToken(String sinaWeiboToken) {
			SinaWeiboToken = sinaWeiboToken;
		}

		public void setSinaWeiboExpireTime(String sinaWeiboExpireTime) {
			SinaWeiboExpireTime = sinaWeiboExpireTime;
		}

		public String getSinaWeiboExpireTime() {
			return SinaWeiboExpireTime;
		}
	}

	public class BindUser implements Serializable {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private String Type;
		private String Status;
		private String ErrorCode;
		private String Message;
		private SinaDetailInfo sinadetailinfo;

		public String getType() {
			return Type;
		}

		public void setType(String type) {
			Type = type;
		}

		public String getStatus() {
			return Status;
		}

		public void setStatus(String status) {
			Status = status;
		}

		public String getErrorCode() {
			return ErrorCode;
		}

		public void setErrorCode(String errorCode) {
			ErrorCode = errorCode;
		}

		public String getMessage() {
			return Message;
		}

		public void setMessage(String message) {
			Message = message;
		}

		public SinaDetailInfo getSinadetailinfo() {
			return sinadetailinfo;
		}

		public void setSinadetailinfo(SinaDetailInfo sinadetailinfo) {
			this.sinadetailinfo = sinadetailinfo;
		}
	}

}
